package com.topn;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class Tkey implements WritableComparable<Tkey> {

    private int year;
    private int month;
    private int day;
    private int temp;
    private String location;

    public Tkey() {
    }

    public Tkey(int year, int month, int day, int temp) {
        this.year = year;
        this.month = month;
        this.day = day;
        this.temp = temp;
    }

    /*
            按照 年、月、日 从低到高排序
             */
    @Override
    public int compareTo(Tkey o) {
        // 先比较年
        int yCompare = Integer.compare(year, o.getYear());

        if (yCompare == 0) { // 年份相同
            // 比较月
            int mCompare = Integer.compare(month, o.getMonth());
            if (mCompare == 0) {// 月相同
                // 比较日
                return Integer.compare(day, o.getDay());
            }
            // 返回月份比较的结果
            return mCompare;
        }

        // 年不同， 直接返回year的比较结果
        return yCompare;
    }

    // 序列化 和 反序列化 字段的顺序一定要保持一致
    // 序列化
    @Override
    public void write(DataOutput out) throws IOException {
        out.writeInt(year);
        out.writeInt(month);
        out.writeInt(day);
        out.writeInt(temp);
        out.writeUTF(location);
    }

    // 反序列化
    @Override
    public void readFields(DataInput in) throws IOException {
        this.year = in.readInt();
        this.month = in.readInt();
        this.day = in.readInt();
        this.temp = in.readInt();
        this.location = in.readUTF();
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public int getTemp() {
        return temp;
    }

    public void setTemp(int temp) {
        this.temp = temp;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    @Override
    public String toString() {
        return "Tkey{" +
                "year=" + year +
                ", month=" + month +
                ", day=" + day +
                ", temp=" + temp +
                ", location='" + location + '\'' +
                '}';
    }
}
